/*
package com.xbsg.protocol.xf.server;

import com.xbsg.protocol.rabbitmq.MqttGateway;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.xbsg.protocol.xf.server.SocketHandler.register;

@Slf4j
@Component
public class SocketServer implements CommandLineRunner, DisposableBean {

    @Value("${server.tcp.port}")
    private int port;

    private boolean started;
    private ServerSocket serverSocket;
    // 防止重复创建socket线程链接对象浪费资源
    private ExecutorService executorService = Executors.newCachedThreadPool();

//    @Autowired
//    private MqttGateway mqttGateway;

    public void start(){
        start(port);
    }

    private void start(Integer port){
        log.info("port: {}", this.port);
        try {
            serverSocket =  new ServerSocket( port);
//            if(mqttGateway == null){
//                //mqttGateway = SpringUtils.getBean(MqttGateway.class);
//                log.info("mqttGateway is null!");
//            }
            started = true;
            log.info("Socket服务已启动，占用端口： {}", serverSocket.getLocalPort());
        } catch (IOException e) {
            log.error("端口冲突,异常信息：{}", e);
            System.exit(0);
        }


        while  (started){
            try {
                // 开启socket监听
                Socket socket = serverSocket.accept();
                //ClientSocket register = register(socket,mqttGateway);
                ClientSocket register = register(socket);
                // 在此判断是否重复创建socket对象线程
                if (register != null){
                    executorService.submit(register);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void stop(){
        this.started = false;
        try {
            new Socket("localhost",this.port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //释放线程池 ExecutorService
        executorService.shutdown();
        try{
            if(serverSocket != null){
                serverSocket.close();
                serverSocket = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void run(String... args) throws Exception {
        start(port);
    }

    @Override
    public void destroy() throws Exception {
        log.info("应用正在关闭，清理相关数据");
        this.stop();
    }
}
*/
